package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbBrandMapper;
import com.dongdong.model.TbBrand;
import com.dongdong.model.TbBrandExample;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.service.BrandService;
import com.dongdong.vo.BrandVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper brandMapper;

    /**
     * 查询品牌类型
     * @return
     */
    @Override
    public List<BrandVO> FindBrandList() {
        //查询品牌类型
        List<TbBrand> tbBrands = brandMapper.selectByExample(null);

        //将查询到的数据转为stream流在转为BrandVO
        List<BrandVO> vos = tbBrands.stream().map((x) -> {
            //创建一个BrandVO对象
            BrandVO brandVO = new BrandVO();
            //将查询到的数据copy到brandVO
            BeanUtils.copyProperties(x, brandVO);
            //返回brandVO
            return brandVO;
            //转为list类型
        }).collect(Collectors.toList());
        return vos;
    }

    /**
     * 分页模糊查询
     * @param bean
     * @return
     */
    @Override
    public PageResult<BrandVO> FindBrandPage(PageBean<BrandVO> bean) {
        TbBrandExample tbBrandExample = new TbBrandExample();
        TbBrandExample.Criteria criteria = tbBrandExample.createCriteria();
        //先判断传入的name和fistChar是否有值
        if (!StrUtil.isEmpty(bean.getParms().getName())){
            criteria.andNameLike("%" + bean.getParms().getName() + "%");
        }
        if (!StrUtil.isEmpty(bean.getParms().getFirstChar())){
            criteria.andFirstCharLike("%" + bean.getParms().getFirstChar() + "%");
        }
        //假如分页代码
        PageHelper.startPage(bean.getPageNumber(),bean.getPageSize());
        List<TbBrand> tbBrands = brandMapper.selectByExample(tbBrandExample);
        PageInfo<TbBrand> info = new PageInfo<>(tbBrands);
        //将查询到的数据转为stream流在转为BrandVO
        List<BrandVO> vos = info.getList().stream().map((x) -> {
            //创建一个BrandVO对象
            BrandVO brandVO = new BrandVO();
            //将查询到的数据copy到brandVO
            BeanUtils.copyProperties(x, brandVO);
            return brandVO;
            //转为list类型
        }).collect(Collectors.toList());
        //返回分页查询
        return new PageResult<>(info.getPageNum(),info.getPageSize(),info.getTotal(),vos);
    }

    /**
     * 增加品牌
     * @param brand
     */
    @Override
    public void InsertBrand(TbBrand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 修改品牌
     * @param brand
     */
    @Override
    public void UpdateBrand(TbBrand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 删除品牌
     * @param id
     */
    @Override
    public void DeleteBrandById(Long id) {
        brandMapper.deleteByPrimaryKey(id);
    }
}
